<script>
	var url;
	$(document).ready(function(){
	
		newData = function (){
			$('#dialog').dialog({
				title: 'Tambah Jadwal Kuliah',
				width: 650,
				height: 450,
				closed: true,
				cache: false,
				href: base_url+'jadwal_kuliah/add/'+$('#s_id_tahunajaran').val()+'/'+$('#s_kd_fakultas').val()+'/'+$('#s_kd_prodi').val(),
				modal: true
			});
			 
			$('#dialog').dialog('open');
			url = base_url+'jadwal_kuliah/save/add/';
		}
		// end newData
		
		editData = function (val){
			// var row = $('#dg').datagrid('getSelected');
			// if (row){
				$('#dialog').dialog({
					title: 'Edit Jadwal Kuliah',
					width: 650,
					height: 430,
					closed: true,
					cache: false,
					href: base_url+'jadwal_kuliah/edit/'+val,
					modal: true
				});
				
				$('#dialog').dialog('open');  
				url = base_url+'jadwal_kuliah/save/edit';
			// }
		}
		//end editData
		
		deleteData = function (val){
			// var row = $('#dg').datagrid('getSelected');
			// if(row){
				if(confirm("Apakah yakin akan menghapus data '" + val + "'?")){
					var response = '';
					$.ajax({ type: "GET",
						 url: base_url+'jadwal_kuliah/delete/' + val,
						 async: false,
						 success : function(response){
							var response = eval('('+response+')');
							if (response.success){
								$.messager.show({
									title: 'Success',
									msg: 'Data Berhasil Dihapus'
								});
								// reload and close tab
								$('#dg').datagrid('reload');
							} else {
								$.messager.show({
									title: 'Error',
									msg: response.msg
								});
							}
						 }
					});
				}
			// }
		}
		//end deleteData 
		
		saveData = function(){
			$('#kd_prodi').removeAttr('disabled');
			$('#form1').form('submit',{
				url: url,
				onSubmit: function(){
					return $(this).form('validate');
				},
				success: function(result){
					//alert(result);
					var result = eval('('+result+')');
					if (result.success){
						$('#dialog').dialog('close');		// close the dialog
						$('#dg').datagrid('reload');		// reload the user data
					} else {
						$.messager.show({
							title: 'Error',
							msg: result.msg
						});
					}
				}
			});
		}
		//end saveData
		
		// action button
		actionbutton = function(value, row, index){
			var col;
			//if (row.kd_jadwal_kuliah != null) {
				<?if($this->mdl_auth->CekAkses(array('menu_id'=>36, 'policy'=>'EDIT'))){?>
				col = '<a href="#" onclick="editData(\''+row.id_jadwal+'\');" class="easyui-linkbutton" iconCls="icon-edit" plain="false">Edit</a>';
				<?}?>
				col += '&nbsp;&nbsp;|&nbsp;&nbsp;';
				<?if($this->mdl_auth->CekAkses(array('menu_id'=>36, 'policy'=>'DELETE'))){?>
				col += '<a href="#" onclick="deleteData(\''+row.id_jadwal+'\');" class="easyui-linkbutton" iconCls="icon-edit" plain="false">Delete</a>';
				<?}?>
			//}
			return col;
		}
		
		$(function(){
			$('#dg').datagrid({
				url:"<?=base_url()?>jadwal_kuliah/grid"
			});
		});
		
		
		// onchange
		$('#s_kd_fakultas').change(function(){
			$('#s_kd_prodi').load(base_url+'prosedur/getProdibyFakultas/'+$('#s_kd_fakultas').val());
		});
		
		// filter
		filter = function(){
			$('#dg').datagrid('load',{
				kd_fakultas : $('#s_kd_fakultas').val(),
				kd_prodi : $('#s_kd_prodi').val(),
				id_tahunajaran : $('#s_id_tahunajaran').val(),
				kd_matakuliah : $('#s_kd_matakuliah').val(),
				kd_dosen : $('#s_kd_dosen').val()
			});
		}
		
		// formater
		getHari = function (value,row, index){
			switch(value){
				case '0':return 'Minggu'; break;
				case '1':return 'Senin'; break;
				case '2':return 'Selasa'; break;
				case '3':return 'Rabu'; break;
				case '4':return 'Kamis'; break;
				case '5':return 'Jum\'at'; break;
				case '6':return 'Sabtu'; break;
			}
		}
		
		getWaktu = function(value, row, index){
			return row.JamMulai.substring(0, 5)+' - '+row.JamSelesai.substring(0, 5);
		}
		
		// expand all group
		expandAllGroup = function(data){
			//alert(JSON.stringify(data));
			for(var i=0; i<data.total; i++){
				$('#dg').datagrid('expandGroup', i);
			}
		}
		
		//# Tombol Bawah
		$(function(){
			var pager = $('#dg').datagrid().datagrid('getPager');	// get the pager of datagrid
			pager.pagination({
				buttons:[
					<?if($this->mdl_auth->CekAkses(array('menu_id'=>36, 'policy'=>'ADD'))){?>
					{
						iconCls:'icon-add',
						text:'Tambah Data',
						handler:function(){
							newData();
						}
					}
					<?}?>
				]
			});			
		});
		
	});
</script>
<table id="dg" title="Kelola Data jadwal_kuliah" data-options="
			rownumbers:true,
			singleSelect:true,
			autoRowHeight:false,
			pagination:true,
			pageSize:30,
			fit:true,
			toolbar:'#toolbar',
			view:groupview,
			groupField:'id_hari',
			groupFormatter:function(value, rows){
				return 'Hari ' + getHari(value);
			},
			onLoadSuccess: expandAllGroup
		">
	<thead>
		<tr>
			<th field="id_jadwal" hidden="true" sortable="false" width="40">ID </th>
			<th field="id_hari" width="100" align="center" hidden="true" formatter="getHari">Hari</th>
			<th field="waktu" width="100" align="center" formatter="getWaktu">Waktu</th>
			<th field="JamMulai" hidden="true"></th>
			<th field="JamSelesai" hidden="true"></th>
			<th field="nama_ruang" width="100" align="center">Ruang</th>
			<th field="kd_matakuliah" width="100" align="center">Kode MK</th>
			<th field="Nama" width="180">Nama Mata Kuliah</th>
			<th field="kd_kelas" width="200">Kelas</th>
			<th field="kd_jenisjadwal" width="30" align="center">Jns</th>
			<th field="SKS" width="40" align="center">SKS</th>
			<th field="nama_dosen" width="150">Dosen</th>
			<th field="JumlahMhsw" width="50" align="center" hidden="true">Jml Mhs</th>
			<th field="action" align="center" formatter="actionbutton" width="90">Aksi</th>
		</tr>
	</thead>
</table>
<div id="toolbar" style="padding:5px;height:auto">
	<div>
		<table width="930" border="0">
		  <tr>
			<td>Tahun Ajaran</td>
			<td>: 
				<select id="s_id_tahunajaran" name="s_id_tahunajaran" style="width:220px;" disabled="disabled">
					<?=$this->mdl_prosedur->OptionTahun(array('value' => $pilihanTahun));?>
				</select>
			</td>
			<td>Fakultas</td>
			<td>: 
				<select id="s_kd_fakultas" name="s_kd_fakultas" style="width:200px;">
					<?=$this->mdl_prosedur->OptionFakultas();?>
				</select>
			</td>
			<td>Kode MK</td>
			<td>: 
				<input name="s_kd_matakuliah" id="s_kd_matakuliah" size="15">
			</td>
		  </tr>
		  <tr>
			<td>&nbsp;</td>
			<td>&nbsp;&nbsp;<a href="#" onclick="filter()" class="easyui-linkbutton" iconCls="icon-search">Filter</a></td>
			<td>Program Studi</td>
			<td>: 
				<select id="s_kd_prodi" name="s_kd_prodi" style="width:200px;">
					<?=$this->mdl_prosedur->OptionProdi();?>
				</select>
			</td>
			<td>Dosen</td>
			<td>: 
				<select id="s_kd_dosen" name="s_kd_dosen" style="width:200px;">
					<?=$this->mdl_prosedur->OptionDosen();?>
				</select>
			</td>
		  </tr>
		</table>
	</div>
</div>
